import os

from flask import Flask
from .auth import auth_bp
from .course import course_bp
from .db import init_app
from .index import index_bp
from .clazz import clazz_bp
from .student import student_bp
from .filter import register_filter

BASE_DIR = os.path.abspath(os.path.dirname(__file__))

def create_app(test_config=None):
    app = Flask(__name__)

    app.config.from_mapping(
        # Set the secret key to some random bytes. Keep this really secret!
        SECRET_KEY=b'yunzhi.club',
        DATABASE=os.path.join(BASE_DIR, 'schema.sqlite'),
        SQLALCHEMY_DATABASE_URI='sqlite:///' + os.path.join(BASE_DIR, 'schema.sqlite'),
        SQLALCHEMY_TRACK_MODIFICATIONS=False
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # 注册蓝图
    app.register_blueprint(auth_bp)
    app.register_blueprint(index_bp)
    app.register_blueprint(clazz_bp)
    app.register_blueprint(student_bp)
    app.register_blueprint(course_bp)

    # 注册过滤器
    register_filter(app)

    # init database
    init_app(app)

    return app
